package com.rayo.accesscontrollercopy.module.welcome;

import android.content.Context;
import android.os.Message;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BasePresenter;
import com.rayo.accesscontrollercopy.model.biz.IWelcomeBiz;
import com.rayo.accesscontrollercopy.model.biz.WelcomeBiz;
import com.rayo.accesscontrollercopy.model.callback.OnGetDbListener;
import com.rayo.accesscontrollercopy.model.db.bean.KeyData;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataImp;

import java.util.List;


/**
 * Created by yanghaojie on 16/7/13.
 */
public class WelcomePresenter extends BasePresenter {
    private IWelcomeView iWelcomeView;
    private IWelcomeBiz iWelcomeBiz;

    private static final long LIMIT_TIME = 3 * 1000;
    private boolean isFinish;
    private long start_time;
    private static final int SETTING = 0;
    private static final int MAIN = 1;
    private static final int SHOW_SNACK_BAR = 2;
    private static final int SHOW_DIALOG = 3;

    public WelcomePresenter(Context context, IWelcomeView iWelcomeView) {
        this.init(context);
        this.iWelcomeView = iWelcomeView;
        this.iWelcomeBiz = new WelcomeBiz(context);
        this.isFinish = false;
    }

    @Override
    protected void message(Message msg) {
        switch (msg.what) {
            case SETTING:
                iWelcomeView.goToSetting();
                break;
            case MAIN:
                iWelcomeView.goToMain();
                break;
            case SHOW_SNACK_BAR:
                iWelcomeView.showSnackBar((String) msg.obj);
                break;
            case SHOW_DIALOG:
                iWelcomeView.showAlertDialog((String) msg.obj);
                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {
        this.isFinish = true;
        start_time = System.currentTimeMillis();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while ((System.currentTimeMillis() - start_time < LIMIT_TIME) || !isFinish) {
//                    SystemClock.sleep(500);
//                }
//            }
//        }).start();
        iWelcomeBiz.loadDb(new OnGetDbListener() {
            @Override
            public <T> void onSuccess(T t) {
                if (null == t) {
                    obtainMessage(MAIN).sendToTarget();
                } else {
                    obtainMessage(MAIN).sendToTarget();
                }
            }

            @Override
            public void onFailed(int error) {
                obtainMessage(SHOW_DIALOG, reference.get().getString(R.string.data_wrong)).sendToTarget();
            }
        });
    }
}
